﻿<%@ Page Language="c#" MasterPageFile="../../masterpages/umbracoDialog.Master" Title="Select media item" AutoEventWireup="true" CodeBehind="CreateMediaItem.aspx.cs" Inherits="DigibizAdvancedMediaPicker.CreateMediaItem" %>
<%@ Register TagPrefix="UmbracoControls" Namespace="umbraco.uicontrols" Assembly="controls" %>
<%@ Register TagPrefix="UmbracoControls" Namespace="umbraco.controls" Assembly="umbraco" %>
<%@ Import namespace="DigibizTree" %>
<%@ Import Namespace="umbraco.IO" %>

<asp:Content ID="ContentPage" ContentPlaceHolderID="body" runat="server">
    
    <script type="text/javascript">
        function CheckFields() {

            //Get all the values we need for checks.
            var errorMessage = '';
            var ddlMediaType = document.getElementById('<%=DdlMediaTypes.ClientID%>');
            var selectedMediaTypeId = ddlMediaType.value;
            var selectedMediaTypeText = ddlMediaType.options[ddlMediaType.selectedIndex].text.toLowerCase();
            var hasUploadField = $('#<%=HiddenFieldHasUploadFile.ClientID%>').val();
            var txtName = $('#<%=TxtName.ClientID%>').val();
            var pickerValue = $('#<%=digibizPickerView.PickedValue.ClientID%>').val();
            var umbPath = '<%=IOHelper.ResolveUrl(SystemDirectories.Umbraco)%>';

            //Check if a Media Type has been selected.
            if (selectedMediaTypeId == '-1') {
                errorMessage = "- You need to select a Media Type.\n";
            }

            //Check if a valid file has been selected if the upload field is displayed.
            if (hasUploadField == 'true') {
                var fileName = document.getElementById('<%=UploadFieldClientId%>').value;
                var extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();

                //Check if a file has been selected.
                if (fileName.length == 0) {
                    errorMessage = errorMessage + '- You need to select a file.\n';
                }

                //Don't render the allowed extensions check if no extensions have been passed.
                <%if (RenderAllowedExtensionCheck){%>

                    //Check if the selected file has the allowed extension.
                    else if (('<%=GetAllowedExtensions()%>').indexOf(',' + extension + ',') < 0) {
                        errorMessage = errorMessage + '- No valid file is selected. Only the following extensions are allowed:\n<%=ShowExtensions(Request.QueryString["allowedExtensions"])%>.\n';
                    }

                <%}%>
            }

            //Check if a name has been entered.
            if (txtName.length == 0) {
                errorMessage = errorMessage + '- You need to enter a name.\n';
            }

            //Check if a folder has been selected.
            if (pickerValue.length == 0) {
                errorMessage = errorMessage + "- You need to select a media item.\n";
            }

            //If a media type and file are selected do an ajax call.
            if (selectedMediaTypeId != '-1' && pickerValue.length != 0) {

                //AJAX call to check if the Media Type the user wants to create is allowed under the selected parent media item.
                 $.ajax({
                    type: "POST",
                    async: false,
                    url: umbPath + "/plugins/DigibizAdvancedMediaPicker/DigibizService.asmx/CheckCreateData",
                    data: '{ "nodeId": ' + parseInt(pickerValue) + ', "mediaTypeId": ' + parseInt(selectedMediaTypeId) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                       errorMessage = errorMessage + msg.d.message;
                    }
                });

            }

            //Display an error message if not all fields are valid.
            if (errorMessage != '') {
                alert(errorMessage);
                return false;
            }

            //Find the button and disable it so the user can't press the button again. 
            //After this do the postback manually because the button won't do it after it's disabled.
            var button = document.getElementById('<%=BtnCreate.ClientID%>');
            button.value = "Creating...";
            button.disabled = true;
            __doPostBack(button.name, '');

            return true;
        }

        function SetFileName() {

            //Get the filename from the selected file and display it in the textbox.
            var path = document.getElementById('<%=UploadFieldClientId%>').value;
            var fileName = path.substring(path.lastIndexOf("\\") + 1, path.length);
            var fileNameNoExtension = fileName.substring(0, fileName.lastIndexOf("."));
            $('#<%=TxtName.ClientID%>').val(fileNameNoExtension);
        }

    </script>

    <asp:MultiView ID="MultiViewPage" runat="server" ActiveViewIndex="0">

        <%--Create view.--%>
        <asp:View ID="ViewCreate" runat="server">

            <UmbracoControls:Pane ID="PaneCreate" runat="server">

                <%--Updatepanel that only updates the dropdownlist and not the other fields.--%>
                <asp:UpdatePanel ID="UpdatePanelFileUpload" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        
                        <%--Media type dropdown.--%>
                        <UmbracoControls:PropertyPanel ID="PropMediaType" runat="server" Text="Media Type:">
                            <asp:DropDownList ID="DdlMediaTypes" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DdlMediaTypes_SelectedIndexChanged" Width="200px"></asp:DropDownList>
                        </UmbracoControls:PropertyPanel>
                        
                        <%--Fileupload.--%>
                        <UmbracoControls:PropertyPanel ID="PropFile" runat="server" Text="Choose file:">
                            <asp:PlaceHolder ID="PlaceHolderUpload" runat="server"></asp:PlaceHolder>
                        </UmbracoControls:PropertyPanel>

                        <%--Hidden value to dertermine if an upload field is found.--%>
                        <asp:HiddenField ID="HiddenFieldHasUploadFile" runat="server" />

                    </ContentTemplate>
                </asp:UpdatePanel>

                <%--Name.--%>
                <UmbracoControls:PropertyPanel ID="PropName" runat="server" Text="Name:">
                    <asp:TextBox ID="TxtName" runat="server" Width="195px"></asp:TextBox>
                </UmbracoControls:PropertyPanel>
                        
                <%--Media tree.--%>
                <UmbracoControls:PropertyPanel ID="PropFolder" runat="server" Text="Parent:">
                    <asp:PlaceHolder ID="PlaceholderPicker" runat="server"></asp:PlaceHolder>
                </UmbracoControls:PropertyPanel>

            </UmbracoControls:Pane>

            <br />
            <p>
                <asp:Button ID="BtnCreate" runat="server" OnClick="BtnCreate_Click" Text="Create" OnClientClick="return CheckFields();"/>
                <em> or </em><a href="#" style="color: blue" onclick="UmbClientMgr.closeModalWindow()"><%=umbraco.ui.Text("general", "cancel", this.getUser())%></a>  
            </p>
        </asp:View>

        <%--Succesfully created view.--%>
        <asp:View ID="ViewSucces" runat="server">
            
            <UmbracoControls:Pane ID="PaneSucces" runat="server">

                The new media item is created succesfully.

                <br />
                <br />
                
                <%--Display the name and media icon/image.--%>
                <asp:Literal ID="LitName" runat="server"></asp:Literal><br /><br />
                <asp:HyperLink ID="HplNewMediaItem" runat="server" Target="_blank">
                    <asp:Image ID="ImgNewMediaItem" runat="server" />
                </asp:HyperLink>

                <br />
                <br />

                <asp:Label ID="LblMessage" runat="server"></asp:Label>

            </UmbracoControls:Pane>
            
            <br />
            <p>
                <asp:PlaceHolder ID="PlaceHolderSelect" runat="server">
                    <input type="button" onclick="javascript:GetNodeId(); return false;" value="Select" />
                    &nbsp;-&nbsp
                </asp:PlaceHolder>
                <asp:Button ID="BtnCreateAgain" runat="server" OnClick="BtnCreateAgain_Click" Text="Create"/>
                <em> or </em><a href="#" style="color: blue" onclick="UmbClientMgr.closeModalWindow()"><%=umbraco.ui.Text("general", "cancel", this.getUser())%></a>  
            </p>
        </asp:View>
    </asp:MultiView>
</asp:Content>